package com.sneakxy.mybase4j.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

/**
 * 分页查询
 * @author 潜行的青衣
 * @see
 */
public class PageSearch {

	private int pageNumber;
	
	private int pageSize;
	
	private String searchText;
	
	private String sortName;
	
	private String sortOrder = "asc";
	
	public Pageable toPageable() {
		Sort sort = null;
		PageRequest pageable = null; 
		int page = this.pageNumber > 0 ? this.pageNumber - 1 : 0;
		if(StringUtils.isNotBlank(this.sortName)) {
			sort = new Sort(StringUtils.equalsIgnoreCase("desc", sortOrder) ? Direction.DESC : Direction.ASC, StringUtils.trimToEmpty(sortName));
			pageable = new PageRequest(page, this.pageSize, sort);
		} else {
			pageable = new PageRequest(page, this.pageSize);
		}
		return pageable;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public String getSearchText() {
		return searchText;
	}

	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

	public String getSortName() {
		return sortName;
	}

	public void setSortName(String sortName) {
		this.sortName = sortName;
	}


	public String getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}
	
}
